/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.expert.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.expert.domain.dto.ExpertBoxSeekDto;
import com.fzhucc.modules.expert.domain.dto.ExpertMsgBoxQueryCriteria;
import com.fzhucc.modules.expert.service.ExpertMsgBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yyg
 * @date 2023-03-10
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "专家库:咨询专家留言箱管理")
@RequestMapping("/api/expertMsgBox")
public class ExpertMsgBoxController {

    private final ExpertMsgBoxService expertMsgBoxService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('expertMsgBox:list')")
    public void exportExpertMsgBox(HttpServletResponse response, ExpertMsgBoxQueryCriteria criteria) throws IOException {
        expertMsgBoxService.download(expertMsgBoxService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询咨询专家留言箱")
    @ApiOperation("查询咨询专家留言箱")
    @PreAuthorize("@el.check('expertMsgBox:list')")
    public ResponseEntity<Object> queryExpertMsgBox(ExpertMsgBoxQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(expertMsgBoxService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除咨询专家留言箱")
    @ApiOperation("删除咨询专家留言箱")
    @PreAuthorize("@el.check('expertMsgBox:del')")
    public ResponseEntity<Object> deleteExpertMsgBox(@RequestBody Integer[] ids) {
        expertMsgBoxService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("咨询专家")
    @ApiOperation("咨询专家")
    @AnonymousPostMapping(value = "/seek")
    public ResponseEntity<Object> seek(@Validated @RequestBody ExpertBoxSeekDto resources) {
        return new ResponseEntity<>(expertMsgBoxService.seek(resources), HttpStatus.OK);
    }
}